$(function () {


    layui.use(['layer', 'form', 'jquery', 'laydate'], function () {
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
        var form = layui.form,
            $ = layui.jquery;


        //下拉选择框的监听
        form.on('select(em_sex)', function (data) {
            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
        });

        laydate.render({
            elem: '#cu_time',
            type: 'datetime'
        });

        //取消并关闭页面
        $("#colseBtn").click(function (data) {
            //parent.location.reload(); // 父页面刷新
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false;
        });

        //监听submit提交按钮 button ，lay-filter 为 save 的
        form.on('submit(save)', function (data) {
            //此段代码可以获取提交的数据
            /* 	layer.alert(JSON.stringify(data.field), {
                     title: '最终的提交信息'
                 }) */
            console.log(JSON.stringify(data.field));
            //ajax 提交
            $.ajax({
                type: "POST",
                dataType: "JSON",
                contentType: "application/json",
                async: "false",
                url: "../user/updateUser", //url
                data: JSON.stringify(data.field), //表单数据
                success: function (result) {
                    if (result.msg = "success") {
                        layer.msg('添加成功');
                        //延迟1秒执行，目的是让用户看到提示
                        setTimeout(function () {
                            //1、先得到当前iframe层（弹出层）的索引  ///2、提交成功关闭弹出层窗口
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }, 1 * 1000);
                    }
                    ;
                },
                error: function () {
                    layer.msg('后台异常！未添加成功');
                }
            });
            //阻止页面跳转
            return false;
        });

        //自定义表单验证
        //lay-verify ：对应userr、pass、等，required 为必填项
        form.verify({
            user: function (value) {
                if (value.length < 2) {
                    return '用户名至少得2个字符啊';
                }
            },
            pass: [
                /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
            ],
            content: function (value) {
                layedit.sync(editIndex);
            }
        });

    });


    // var username = UrlParam.paramValues("username");
    // var userid = UrlParam.paramValues("userid");
    var username = sessionStorage.getItem("username");
    var userid = sessionStorage.getItem("userid");
    $("input[name='id']").val(userid);
    // alert(username);

    $.ajax({
        url: "../user/findByName",
        type: "post",
        async: "false",
        traditional: true,
        // dataType: "json",
        data: {
            "u_name": username,
            "page": 1,
            "limit": 10
        },
        success: function (data) {
            // $("input[name='keleyicom']")
            // alert(data.data[0]);
            // alert(data.data.u_name);
            $("input[name='u_name']").val(data.data[0].u_name);
            if (data.data[0].u_sex == "男") {

                $("input[name='u_sex'][value='男']").attr("checked", true);
            } else {
                $("input[name='u_sex'][value='女']").attr("checked", true);
            }
            $("input[name='u_birthday']").val(data.data[0].u_birthday);
            $("input[name='u_age']").val(data.data[0].u_age);
            $("input[name='u_phone']").val(data.data[0].u_phone);
            // $("input[name='u_email']").val();
            $("input[name='u_nation']").val(data.data[0].u_nation);
            $("input[name='u_politics']").val(data.data[0].u_politics);
            $("input[name='u_entry_time']").val(data.data[0].u_entry_time);

            if (data.data[0].u_department == "人事部") {
                $("option[value='人事部']").attr("selected", true);
            } else if (data.data[0].u_department == "市场部") {
                $("option[value='市场部']").attr("selected", true);
            } else if (data.data[0].u_department == "企划部") {
                $("option[value='企划部']").attr("selected", true);
            } else if (data.data[0].u_department == "财务部") {
                $("option[value='财务部']").attr("selected", true);
            } else {
                $("option[value='4']").attr("selected", true);
            }

            if (data.data[0].u_job == "销售") {
                $("option[value='销售']").attr("selected", true);
            } else if (data.data[0].u_job == "客服") {
                $("option[value='客服']").attr("selected", true);
            } else if (data.data[0].u_job == "经理") {
                $("option[value='经理']").attr("selected", true);
            } else if (data.data[0].u_job == "主任") {
                $("option[value='主任']").attr("selected", true);
            } else {
                $("option[value='管事']").attr("selected", true);
            }


            // $("select[name='u_job'] > option").each(function (index) {
            //     if (data.data[0].u_job == this.value) {
            //         this.attr("selected", true);
            //     }
            // });

            $("select[name='u_superior'] > option").each(function (index) {
                // alert(data.data[0].u_superior);
                if (data.data[0].u_superior == this.value) {
                    $(this).attr("selected", true);
                }
            });

            // if (data.data[0].u_superior=="张三"){}
            $("select[name='u_superior']")

            $("input[name='u_job_number']").val(data.data[0].u_job_number);

            if (data.data[0].u_ismarry == "是") {

                $("input[name='u_ismarry'][value='是']").attr("checked", true);
            } else {
                $("input[name='u_ismarry'][value='否']").attr("checked", true);
            }

            if (data.data[0].u_health == "是") {

                $("input[name='u_health'][value='是']").attr("checked", true);
            } else {
                $("input[name='u_health'][value='否']").attr("checked", true);
            }

            $("input[name='u_education']").val(data.data[0].u_education);


        }

    })
})

